<template>

  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.showClose"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form ref="formBook" :rules="rulesMusic" :model="formMusic" label-width="180px">
      <el-form-item label="歌曲名称" prop="name" >
        <el-input v-model.trim="formMusic.name"></el-input>
      </el-form-item>
      <el-form-item label="歌手" prop="singer" >
        <el-input v-model.trim="formMusic.singer"></el-input>
      </el-form-item>
      <el-form-item label="作词" prop="lyrics" >
        <el-input v-model.trim="formMusic.lyrics"></el-input>
      </el-form-item>
      <el-form-item label="专辑" prop="album" >
        <el-input v-model.trim="formMusic.album"></el-input>
      </el-form-item>
      <el-form-item label="音乐类型" prop="typeId">
        <el-select v-model="formMusic.typeId" placeholder="请选择音乐类型">
          <el-option
            v-for="item in musicTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评分" prop="grade" >
        <el-rate v-model="formMusic.grade" :texts="texts" show-text></el-rate>
      </el-form-item>
      <el-form-item label="点评" prop="comment" >
        <el-input v-model.trim="formMusic.comment" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="发行时间" prop="dateOfIssue" >
        <!--这个组件会报 Avoid mutating a prop directly since the,添加 placement="bottom-start" -->
        <el-date-picker
          v-model="formMusic.dateOfIssue"
          type="date"
          placement="bottom-start"
          placeholder="选择发行日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd" v-if="defaultValue.saveUpdSelect !== 'select'">{{defaultValue.title}}</el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>

</template>

<script>

  import http from '@/utils/http';
  import {clearInvalid, getFullDate} from "../../utils/utils"

  export default {
    name: "MusicSaveUpd.vue",
    data(){
      return {
        // 默认值，进入页面后的值
        defaultValue: {
          // 弹出框标题
          title: '查看',
          // 保存还是修改or查看,取值有 select | save | upd
          saveUpdSelect: 'select'
        },
        // 表格的默认值
        tableConfig: {
          // 关闭时销毁 Dialog 中的元素
          destroyOnClose: true,
          // 弹出框是否打开
          show: false,
          // 是否显示关闭按钮
          showClose: true,
          // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
          lockScroll: false,
          appendToBody: true,
        },
        // 要提交的数据
        formMusic: {
          name: null,
          typeId: null,
          album: null,
          singer: null,
          lyrics: null,
          dateOfIssue: null,
          comment: null,
          grade: null,
        },
        // 校验规则
        rulesMusic: {
          name: [{ required: true, message: '请输入歌曲名称', trigger: 'blur' }],
          singer: [{ required: true, message: '请输入歌手', trigger: 'blur' }],
          grade: [{ required: true, message: '请输入评分', trigger: 'blur' }],
          typeId: [{ required: true, message: '请选择音乐类型', trigger: 'change' }],
        },
        // 音乐类型
        musicTypeList: [],
        // 评价文字说明
        texts: ['浪费时间', '勉强入口', '普通', '优秀', '经典'],
      }
    },
    methods: {
      /**
       * 是保存还是修改音乐
       */
      toSaveUpd(){
        if (this.defaultValue.saveUpdSelect === 'save'){
          this.saveMusic()

        } else if (this.defaultValue.saveUpdSelect === 'upd'){
          this.updMusic()

        } else {

        }
      },
      /**
       * 保存音乐
       */
      saveMusic(){
        http.post("/music/saveMusic", this.formMusic).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("保存成功");
          this.$emit('getMusicListPage', null)
          // 关闭弹出框
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 更新音乐
       */
      updMusic(){
        clearInvalid(this.formMusic)
        http.post("/music/updMusic", this.formMusic).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("修改成功");
          this.$emit('getMusicListPage', null)
          // 关闭弹出框
          this.initMusic()
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 打开弹出框之前
       * @param value 保存还是修改or查看,取值有 select \ save \ upd
       * @param obj 如果是修改和查看，则是详细数据,需要赋值
       * @param type 音乐类型
       */
      openPageMusic(value, obj, type){
        // 从外部转入，减少查询
        this.musicTypeList = type
        this.defaultValue.saveUpdSelect = value
        this.initMusic()

        if (value === 'save'){
          this.defaultValue.title = '保存音乐'

        } else if (value === 'upd'){
          this.defaultValue.title = '修改音乐'
          this.formMusic = JSON.parse(JSON.stringify(obj))
          if (this.formMusic.dateOfIssue){
            // 转换时间戳为日期
            this.formMusic.dateOfIssue = getFullDate(this.formMusic.dateOfIssue)
          }

        } else {

        }

        this.tableConfig.show = true
        this.$store.commit('navigationListChange', ['我的音乐', this.defaultValue.title])
      },
      /**
       * 初始化音乐
       */
      initMusic(){
        this.formMusic = {
          name: null,
          typeId: null,
          album: null,
          singer: null,
          lyrics: null,
          dateOfIssue: null,
          comment: null,
          grade: null
        }
      },
    },
    mounted(){

    },
  }
</script>

<style scoped>

</style>
